package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class Age {
    public static int AgeOfAcquisition(String dateString){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDate = null;
        try {
            birthDate = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar birthDateCal = Calendar.getInstance();
        birthDateCal.setTime(birthDate);
        Calendar nowCal = Calendar.getInstance();

        int age = nowCal.get(Calendar.YEAR) - birthDateCal.get(Calendar.YEAR);
        if (nowCal.get(Calendar.MONTH) < birthDateCal.get(Calendar.MONTH) ||
                (nowCal.get(Calendar.MONTH) == birthDateCal.get(Calendar.MONTH) &&
                        nowCal.get(Calendar.DATE) < birthDateCal.get(Calendar.DATE))) {
            age--;
        }
        return age;
    }


    public static String GetLocalTime(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime;
        formattedDateTime = now.format(formatter);
        return formattedDateTime;
    }

}
